﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using WebBanThuoc.DTO;

namespace WebBanThuoc.DAO
{
    public class NhaSanXuat_DAO
    {
        private SqlConnection connection;
        //public bool hasData { set; get; }
        public NhaSanXuat_DAO(SqlConnection connection)
        {
            this.connection = connection;
        }

        private List<NhaSanXuat> getListBy(string whereCom, string orderCom)
        {
            List<NhaSanXuat> data = new List<NhaSanXuat>();
            string comStr = "Select * from [NhaSanXuat]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        NhaSanXuat obj = new NhaSanXuat();

                        obj.MaNhaSanXuat = dap.DaReader["MaNhaSanXuat"].ToString();
                        obj.TenNhaSanXuat = dap.DaReader["TenNhaSanXuat"].ToString();
                        obj.DiaChi = dap.DaReader["DiaChi"].ToString();
                        obj.Email = dap.DaReader["Email"].ToString();
                        obj.IsStatus = Convert.ToInt32(dap.DaReader["IsStatus"]);                        

                        data.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public List<NhaSanXuat> getListByKey()
        {
            return getListBy("", " [MaNhaSanXuat] asc");
        }

        private int delete(string whereCom)
        {
            string comUpdate = "DELETE FROM [NhaSanXuat] ";
            if (whereCom.Trim() != "")
                comUpdate += " where " + whereCom;
            try
            {
                IDataProvider dap = new DataProvider(this.connection);
                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comUpdate);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int deleteByKey(string MaNhaSanXuat)
        {
            return delete(" [MaNhaSanXuat]='" + MaNhaSanXuat + "' ");
        }

        public int add(NhaSanXuat obj)
        {
            string comQuery = "INSERT INTO [NhaSanXuat] ([MaNhaSanXuat],[TenNhaSanXuat]"
               + ",[DiaChi],[Email],[IsStatus])"
               + " VALUES (@MaNhaSanXuat,@TenNhaSanXuat,@DiaChi"
               + ",@Email,@IsStatus)";

            try
            {
                IDataProvider dap = new DataProvider(connection);

                dap.AddParameters("@MaNhaSanXuat", obj.MaNhaSanXuat, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@TenNhaSanXuat", obj.TenNhaSanXuat, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@DiaChi", obj.DiaChi, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@Email", obj.Email, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@IsStatus", obj.IsStatus, System.Data.SqlDbType.Int);

                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private int update(NhaSanXuat obj, string whereCom)
        {

            string comQuery = "UPDATE [NhaSanXuat] SET [MaNhaSanXuat]=@MaNhaSanXuat,[TenNhaSanXuat]=@TenNhaSanXuat"
               + ",[DiaChi]=@DiaChi,[Email]=@Email,[IsStatus]=@IsStatus ";
            if (whereCom.Trim() != "")
                comQuery += " where " + whereCom;
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.AddParameters("@MaNhaSanXuat", obj.MaNhaSanXuat, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@TenNhaSanXuat", obj.TenNhaSanXuat, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@DiaChi", obj.DiaChi, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@Email", obj.Email, System.Data.SqlDbType.NVarChar);
                dap.AddParameters("@IsStatus", obj.IsStatus, System.Data.SqlDbType.Int);

                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int updateByKey(NhaSanXuat data, string MaNhaSanXuat)
        {
            return update(data, " [MaNhaSanXuat]='" + MaNhaSanXuat + "' ");
        }

        public string createMaNhaSanXuat()
        {
            string result = "";
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, "SELECT [dbo].[fnc_CreateNew_MaNhaSanXuat] () as col");
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    dap.DaReader.Read();
                    result = dap.DaReader["col"].ToString().Trim();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private NhaSanXuat getBy(string whereCom)
        {
            NhaSanXuat obj = null;
            string comStr = "Select * from [NhaSanXuat]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        obj = new NhaSanXuat();

                        obj.MaNhaSanXuat = dap.DaReader["MaNhaSanXuat"].ToString();
                        obj.TenNhaSanXuat = dap.DaReader["TenNhaSanXuat"].ToString();
                        obj.DiaChi = dap.DaReader["DiaChi"].ToString();
                        obj.Email = dap.DaReader["Email"].ToString();
                        obj.IsStatus = Convert.ToInt32(dap.DaReader["IsStatus"]);


                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return obj;

        }

        public NhaSanXuat getByKey(string id)
        {
            return getBy("[MaNhaSanXuat]='" + id + "'");
        }
    }
}